package com.freexiaoyu.cloud.utils

import java.security.InvalidKeyException
import java.security.Key
import java.security.NoSuchAlgorithmException
import java.security.spec.InvalidKeySpecException
import javax.crypto.BadPaddingException
import javax.crypto.Cipher
import javax.crypto.IllegalBlockSizeException
import javax.crypto.NoSuchPaddingException
import javax.crypto.spec.SecretKeySpec

object CodeHelper {
    //DES解密
    @Throws(
        InvalidKeyException::class,
        InvalidKeySpecException::class,
        NoSuchAlgorithmException::class,
        NoSuchPaddingException::class,
        IllegalBlockSizeException::class,
        BadPaddingException::class
    )
    fun DesDecrypt(byteMi: ByteArray, byteKey: ByteArray): ByteArray {
        return Des(byteMi, byteKey, Cipher.DECRYPT_MODE)
    }

    //DES加密
    @Throws(
        InvalidKeyException::class,
        NoSuchAlgorithmException::class,
        InvalidKeySpecException::class,
        NoSuchPaddingException::class,
        IllegalBlockSizeException::class,
        BadPaddingException::class
    )
    fun DesEncrypt(byteMi: ByteArray, byteKey: ByteArray): ByteArray {
        return Des(byteMi, byteKey, Cipher.ENCRYPT_MODE)
    }

    //DES加解密
    @Throws(
        InvalidKeyException::class,
        NoSuchAlgorithmException::class,
        InvalidKeySpecException::class,
        NoSuchPaddingException::class,
        IllegalBlockSizeException::class,
        BadPaddingException::class
    )
    private fun Des(byteData: ByteArray, byteKey: ByteArray, opmode: Int): ByteArray {
        var cipher: Cipher? = null
        return try {
            val desKey = SecretKeySpec(byteKey, "DES")
            cipher = Cipher.getInstance("DES/ECB/PKCS5Padding")
            cipher.init(opmode, desKey)
            cipher.doFinal(byteData)
        } finally {
            cipher = null
        }
    }

    @Throws(Exception::class)
    fun AesDecrypt(byteMi: ByteArray, byteKey: ByteArray): ByteArray {
        return Aes(byteMi, byteKey, Cipher.DECRYPT_MODE)
    }

    @Throws(Exception::class)
    fun AesEncrypt(byteMi: ByteArray, byteKey: ByteArray): ByteArray {
        return Aes(byteMi, byteKey, Cipher.ENCRYPT_MODE)
    }

    @Throws(Exception::class)
    private fun Aes(byteData: ByteArray, byteKey: ByteArray, opmode: Int): ByteArray {
        var cipher: Cipher? = null
        return try {
            val aesKey = SecretKeySpec(byteKey, "AES")
            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding")
            cipher.init(opmode, aesKey)
            cipher.doFinal(byteData)
        } finally {
            cipher = null
        }
    }

    @Throws(Exception::class)
    fun RsaDecrypt(byteMi: ByteArray, pKey: Key): ByteArray {
        return Rsa(byteMi, pKey, Cipher.DECRYPT_MODE)
    }

    @Throws(Exception::class)
    fun RsaEncrypt(byteMi: ByteArray, pKey: Key): ByteArray {
        return Rsa(byteMi, pKey, Cipher.ENCRYPT_MODE)
    }

    @Throws(Exception::class)
    private fun Rsa(byteData: ByteArray, pKey: Key, opmode: Int): ByteArray {
        var cipher: Cipher? = null
        return try {
            cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding")
            cipher.init(opmode, pKey)
            cipher.doFinal(byteData)
        } finally {
            cipher = null
        }
    }
}